//
//  ActivityViewController.m
//  UI_元旦豆瓣半完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActiviTableViewCell.h"
//#import "ActiTableViewCell.h"
#import "ActivityModel.h"
#import "UIImageView+WebCache.h"
#import "SecondViewController.h"

@interface ActivityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *modelArr;

@end

@implementation ActivityViewController

- (void)dealloc {
    [_modelArr release];
    [_tableView release];
    [_arr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];
    self.navigationItem.title = @"活动";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [_tableView registerNib:[UINib nibWithNibName:@"ActiviTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"myCell"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activity" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    self.modelArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        ActivityModel *model = [ActivityModel activityModel];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArr addObject:model];
    }
    
    
    // 初始化数组
    self.arr = [NSMutableArray array];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActiviTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    ActivityModel *model = _modelArr[indexPath.row];
//    cell.titleLable.text = model.title;
    NSString *begin = [model.begin_time substringWithRange:NSMakeRange(5, 11)];
    NSString *end = [model.end_time substringWithRange:NSMakeRange(5, 11)];
//    cell.timeLable.text = [NSString stringWithFormat:@"%@ -- %@", begin, end];
//    cell.addressLable.text = model.address;
//    cell.category_nameLable.text = model.category_name;
//    cell.wisher_countLable.text = [model.wisher_count stringValue];
//    cell.participant_countLable.text =[model.participant_count stringValue];
//    [cell.actiImage sd_setImageWithURL:[NSURL URLWithString:model.image]];
    cell.title.text = model.title;
    cell.time.text = [NSString stringWithFormat:@"%@ -- %@", begin, end];
    cell.address.text = model.address;
    cell.category_name.text = model.category_name;
    cell.wise.text = [model.wisher_count stringValue];
    cell.part.text = [model.participant_count stringValue];
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:model.image]];

    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 250;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SecondViewController *secVC = [[SecondViewController alloc] init];
    secVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:secVC animated:YES];
    
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    secVC.secDic = result[@"events"][indexPath.row];

    secVC.mArr = _arr;
//    NSLog(@"%@", _arr);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
